//
//  HDCFFundDetailVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/1.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFFundDetailVc.h"
#import "HDCFFundDetailDeader.h"
#import "HDCFFundDetailMiddleView.h"
#import "HDCFFundDetailMiddleCell.h"
#import "HDCFFundDetailFooter.h"
#import "HDCFBuyFundVc.h"
#import "SVProgressHUD.h"
#import "HDCFLoginVc.h"
#import "HDCFOpenAccountVc.h"
#import "HDCFFixThrowViewController.h"//定投vc
#import "Allhelp.h"


#define KColec @"api/Favorite/AddFavorite"
#define KdeleteCole @"api/Favorite/DelFavoriteByFundCode"

@interface HDCFFundDetailVc ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) UIScrollView *mainScroll;
@property (nonatomic, weak) UITableView *middleTable;
@property (nonatomic, weak) UITableView *bottomTabel;
@property (nonatomic, strong) NSDictionary *detailDict;
@property (nonatomic, weak) HDCFFundDetailDeader *headder;
@property (nonatomic, weak) HDCFFundDetailMiddleView *middleView;
@property (nonatomic, strong) NSDictionary *fundTypeDict;
@property (nonatomic, weak) HDCFFundDetailFooter *footer;
@property (nonatomic, strong) NSDictionary *selfEarnDict;
@property (nonatomic, strong) NSDictionary *sameClassEarn;
@property (nonatomic, strong) NSDictionary *markitEarn;

@property (nonatomic, assign) BOOL fundIsCollec;

@property (nonatomic, assign) NSInteger tableRows;
@property (nonatomic, strong) NSDictionary *colectInfoDict;

@property (nonatomic, weak) UIButton *commitBtn;

@property (nonatomic, weak) UIView *clearHudView;
@end

@implementation HDCFFundDetailVc

- (void)setIsLiCai:(BOOL)isLiCai{
    _isLiCai = isLiCai;
}

- (NSDictionary *)detailDict{
    if (!_detailDict) {
        self.detailDict = [NSDictionary dictionary];
    }
    return _detailDict;
}

- (void)setIsColect:(BOOL)isColect{
    _isColect = isColect;
}

- (NSDictionary *)fundTypeDict{
    if (!_fundTypeDict) {
        self.fundTypeDict = @{@"MM":@"货币型",
                              @"BOND":@"债券型",
                              @"MIXED":@"混合型",
                              @"CP":@"保本型",
                              @"EQ":@"股票型",
                              @"AI":@"另类型",
                              @"INDEX":@"指数型",
                              @"ST":@"分级型",
                              @"UNKNOWN":@"其他"
                              };
    }
    return _fundTypeDict;
}

- (void)setDataDict:(NSDictionary *)dataDict{
    _dataDict = dataDict;
}

- (void)setDictTwo:(NSDictionary *)dictTwo{
    _dictTwo = dictTwo;
    NSString *isBuyEnable = dictTwo[@"isBuyEnable"];
    if ([isBuyEnable isEqualToString:@"Y"]){
        
    }else{
        [self.commitBtn setTitle:@"已售罄" forState:UIControlStateNormal];
        [self.commitBtn setBackgroundColor:[UIColor lightGrayColor]];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_isLiCai == YES) {
        [self.navigationItem setTitle:@"理财详情"];
    }else{
        [self.navigationItem setTitle:@"基金详情"];
    }
    
    self.tableRows = 4;
    UIScrollView *mainScrollView = [[UIScrollView alloc] init];
    self.mainScroll = mainScrollView;
    if (_isColect == YES) {
        mainScrollView.frame = CGRectMake(0, 0, screenWidth, screenHeight - 49);
    }else{
        mainScrollView.frame = CGRectMake(0, 0, screenWidth, screenHeight - 49 - KNavHeight);
    }
    
//    mainScrollView.contentSize = CGSizeMake(screenWidth, screenHeight);
    mainScrollView.showsVerticalScrollIndicator = NO;
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mainScrollView];
    
    
    [self initBottomView];
    
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.commitBtn = btn;
//    if (_isColect == YES) {
//        btn.frame = CGRectMake(0, screenHeight - 49, screenWidth, 49);
//    }else{
//        btn.frame = CGRectMake(0, screenHeight - 49 - KNavHeight, screenWidth, 49);
//    }
//    if (_dictTwo) {
//        NSString *isBuyEnable = _dictTwo[@"isBuyEnable"];
//        if ([isBuyEnable isEqualToString:@"Y"]){
//            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//            [btn setTitle:@"立即购买" forState:UIControlStateNormal];
//            [self.commitBtn setBackgroundColor:ZKcolor(184, 27, 36)];
//        }else{
//            [self.commitBtn setTitle:@"已售罄" forState:UIControlStateNormal];
//            [self.commitBtn setBackgroundColor:[UIColor lightGrayColor]];
//        }
//    }else{
//        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [btn setBackgroundColor:ZKcolor(184, 27, 36)];
//        [btn setTitle:@"立即购买" forState:UIControlStateNormal];
//    }
//    [btn addTarget:self action:@selector(jumpBuy) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btn];
    
    [self creatView];
    
    [self initData];
    [self initDataWithSelfEarn];
//    [self initDataWithSameClassEarn];
    [self initDataWithMarkitFundEarn];
    [self getThisFundIsColect];
}
#pragma mark -
#pragma mark InitView
-(void)initBottomView
{
    UIView * bottomView = [UIView new];
    if (_isColect == YES) {
        bottomView.frame = CGRectMake(0, screenHeight - 49, screenWidth, 49);
    }else{
        bottomView.frame = CGRectMake(0, screenHeight - 49 - KNavHeight, screenWidth, 49);
    }
    [self.view addSubview:bottomView];
    
    //定投
    UIButton *fixThrowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    fixThrowBtn.frame = CGRectMake(0, 0 , screenWidth/3.0, 49);
    [fixThrowBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [fixThrowBtn setTitle:@"定投" forState:UIControlStateNormal];
    [fixThrowBtn addTarget:self action:@selector(fixThrowClick) forControlEvents:UIControlEventTouchUpInside];
    [fixThrowBtn setBackgroundColor:ZKcolor(184, 27, 36)];
    [bottomView addSubview:fixThrowBtn];
    
    UILabel * lineOne = [UILabel new];
    lineOne.frame = CGRectMake(screenWidth/3.0-0.5, 0,0.5, 49);
    lineOne.backgroundColor = [UIColor whiteColor];
    [bottomView addSubview:lineOne];
    
    //添加购物车
    UIButton *addShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addShopBtn.frame = CGRectMake(screenWidth/3.0, 0 , screenWidth/3.0, 49);
    [addShopBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addShopBtn setTitle:@"添加购物车" forState:UIControlStateNormal];
    [addShopBtn addTarget:self action:@selector(addShop) forControlEvents:UIControlEventTouchUpInside];
    [addShopBtn setBackgroundColor:ZKcolor(184, 27, 36)];
    [bottomView addSubview:addShopBtn];
    
    UILabel * lineTwo = [UILabel new];
    lineTwo.frame = CGRectMake(screenWidth/3.0*2-0.5, 0,0.5, 49);
    lineTwo.backgroundColor = [UIColor whiteColor];
    [bottomView addSubview:lineTwo];
    
    //立即购买
    UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    buyBtn.frame = CGRectMake(screenWidth/3.0*2, 0 , screenWidth/3.0, 49);
    [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    [buyBtn addTarget:self action:@selector(jumpBuy) forControlEvents:UIControlEventTouchUpInside];
    [buyBtn setBackgroundColor:ZKcolor(184, 27, 36)];
    self.commitBtn = buyBtn;
    [bottomView addSubview:buyBtn];
    
    
    if (_dictTwo) {
        NSString *isBuyEnable = _dictTwo[@"isBuyEnable"];
        if ([isBuyEnable isEqualToString:@"Y"]){
            [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
            [self.commitBtn setBackgroundColor:ZKcolor(184, 27, 36)];
            [addShopBtn setBackgroundColor:ZKcolor(184, 27, 36)];
            [fixThrowBtn setBackgroundColor:ZKcolor(184, 27, 36)];
        }else{
            [self.commitBtn setTitle:@"已售罄" forState:UIControlStateNormal];
            [self.commitBtn setBackgroundColor:[UIColor lightGrayColor]];
            [addShopBtn setBackgroundColor:[UIColor lightGrayColor]];
            [fixThrowBtn setBackgroundColor:[UIColor lightGrayColor]];
        }
    }else{
        [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [buyBtn setBackgroundColor:ZKcolor(184, 27, 36)];
        [addShopBtn setBackgroundColor:ZKcolor(184, 27, 36)];
        [fixThrowBtn setBackgroundColor:ZKcolor(184, 27, 36)];
        [buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    }
}
#pragma mark 查看是否收藏
- (void)getThisFundIsColect{
    NSDictionary *dict;
    if (_dataDict) {
        dict = @{@"fundCode":_dataDict[@"fundCode"]
                  };
    }else{
        dict = @{@"fundCode":_dictTwo[@"productCode"]
                  };
    }
    [HttpRequest sendTwoSelfGetRequestWithURL:@"api/Favorite/IsFavorited" WithParamets:dict success:^(id result) {
        NSNumber *code = result[@"code"];
        if ([code isEqualToNumber:@1]) {
            if ([result[@"result"] isEqual:@1]) {
                self.headder.imgView.image = [UIImage imageNamed:@"icon_shoucang_sel"];
                [self.headder.shoucangBtn setTitle:@"已收藏" forState:UIControlStateNormal];
                self.fundIsCollec = YES;
            }else{
                self.fundIsCollec = NO;
            }
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)initDataWithSameClassEarn{
    
    NSDictionary *param;
    if (_dataDict) {
        param = @{@"fundCode":_dataDict[@"fundCode"]
                  };
    }else{
        param = @{@"fundCode":_dictTwo[@"productCode"]
                  };
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[KparametTwo] = @"/public/product/get-fund-type-performance-by-fund-type";
    NSString *str = [self dictionaryToJson:param];
    NSLog(@"%@", str);
    dict[KparametOne] = [self dictionaryToJson:param];
    [HttpRequest sendGetRequestWithURL:nil WithParamets:dict success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            NSDictionary *dict = result[@"data"];
            
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)initDataWithMarkitFundEarn{
    NSDictionary *param = @{@"indexCode":@"000300.SH"
                            };
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[KparametTwo] = @"/public/product/get-market-index-performance-by-index-code";
    NSString *str = [self dictionaryToJson:param];
    NSLog(@"%@", str);
    dict[KparametOne] = [self dictionaryToJson:param];
    [HttpRequest sendGetRequestWithURL:nil WithParamets:dict success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            self.markitEarn = result[@"data"];
            [self.middleTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)initDataWithSelfEarn{
    
    NSDictionary *param;
    if (_dataDict) {
        param = @{@"fundCode":_dataDict[@"fundCode"]
                  };
    }else{
        param = @{@"fundCode":_dictTwo[@"productCode"]
                  };
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[KparametTwo] = @"/public/product/get-fund-performance-by-fund-code";
    NSString *str = [self dictionaryToJson:param];
    NSLog(@"%@", str);
    dict[KparametOne] = [self dictionaryToJson:param];
    [HttpRequest sendGetRequestWithURL:nil WithParamets:dict success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            NSDictionary *dict = result[@"data"];
            self.selfEarnDict = dict[@"stageReturn"];
            self.sameClassEarn = dict[@"typeAverage"];
            [self.middleTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
- (void)initData{
    [SVProgressHUD show];
    NSDictionary *param;
    if (_dataDict) {
        param = @{@"fundCode":_dataDict[@"fundCode"]
                  };
    }else{
        param = @{@"fundCode":_dictTwo[@"productCode"]
                  };
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[KparametTwo] = @"/public/product/get-single-fund-basic";
    NSString *str = [self dictionaryToJson:param];
    NSLog(@"%@", str);
    dict[KparametOne] = [self dictionaryToJson:param];
    [HttpRequest sendGetRequestWithURL:nil WithParamets:dict success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD dismiss];
            self.detailDict = result[@"data"];
            //LLLog(@"%@",result);
            [self setData];
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)setData{
//    __weak typeof(self) weakSelf = self;
//    weakSelf.header.dataDict = self.detailDict;
    self.headder.dataDict = self.detailDict;
    self.footer.dataDict = self.detailDict;
    self.middleView.dataDict = self.detailDict;
    
    NSLog(@"%@", self.detailDict);
}

#pragma mark  收藏
- (void)colectFund{
    NSString *token = kToken;
    if (!(token.length > 0)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFLoginVc *loginVc = [[HDCFLoginVc alloc] init];
            [self.navigationController pushViewController:loginVc animated:NO];
            
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"FundCode"] = _dataDict[@"fundCode"];
    paramets[@"FundName"] = _dataDict[@"fundName"];
    NSString *jsonStr = [self dictionaryToJson:paramets];
    [HttpRequest sendSelfPostRequestWithURL:KColec WithParamets:jsonStr WithParamet:nil success:^(id result) {
        NSNumber *code = result[@"code"];
        if ([code.stringValue isEqualToString:@"1"]) {
            [SVProgressHUD showSuccessWithStatus:@"收藏成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            self.headder.imgView.image = [UIImage imageNamed:@"icon_shoucang_sel"];
            self.fundIsCollec = YES;
            [SVProgressHUD dismissWithDelay:1.0];
            self.colectInfoDict = result[@"result"];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

//取消收藏
- (void)delColectFund{
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"fundCode"] = _dataDict[@"fundCode"];
    NSString *jsonStr = [self dictionaryToJson:paramets];
    
    [HttpRequest sendSelfPostTwoRequestWithURL:KdeleteCole WithParamets:nil WithParamet:paramets success:^(id result) {
        if ([result[@"code"] isEqual:@1]) {
            self.fundIsCollec = NO;
            [SVProgressHUD showSuccessWithStatus:@"取消成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            self.headder.imgView.image = [UIImage imageNamed:@"icon_shoucang_dis"];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
    
//    [HttpRequest sendSelfPostRequestWithURL:KdeleteCole WithParamets:nil WithParamet:paramets success:^(id result) {
//        self.fundIsCollec = NO;
//        [SVProgressHUD dismissWithDelay:1.0];
//        self.headder.imgView.image = [UIImage imageNamed:@"icon_shoucang_dis"];
//    } failure:^(NSError *errer) {
//        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
//        [SVProgressHUD dismissWithDelay:1.0];
//    }];
}

#pragma mark -
#pragma mark 点击事件
//定投
-(void)fixThrowClick
{
    //判断是登录以及开户
    //[self isLoginAndOpenCount];
    if (_dictTwo) {
        if ([_dictTwo[@"isBuyEnable"] isEqualToString:@"Y"]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您将使用奕丰金融提供的基金销售服务" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                HDCFFixThrowViewController * fixThrowVC = [HDCFFixThrowViewController new];
                fixThrowVC.dataDic = self.detailDict;
                [self.navigationController pushViewController:fixThrowVC animated:YES];
            }];
            UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction: action1];
            [alert addAction: action2];
            [self presentViewController:alert animated:NO completion:nil];
            
        }else{
            [SVProgressHUD showInfoWithStatus:@"已售磬"];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您将使用奕丰金融提供的基金销售服务" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFFixThrowViewController * fixThrowVC = [HDCFFixThrowViewController new];
            fixThrowVC.dataDic = self.detailDict;
            [self.navigationController pushViewController:fixThrowVC animated:YES];
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        
    }
    
}
//添加购物车
-(void)addShop
{
    //判断是登录
    NSString *token = kToken;
    if (!(token.length > 0)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFLoginVc *loginVc = [[HDCFLoginVc alloc] init];
            [self.navigationController pushViewController:loginVc animated:NO];
            
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
    //LLLog(@"添加购物车");
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"UserId"] = @"";
    paramets[@"FundCode"] = _dataDict[@"fundCode"];
    paramets[@"FundName"] = _dataDict[@"fundName"];
    paramets[@"Amount"] = @"";
    paramets[@"UpdateTime"] = [Allhelp geyTime];
    paramets[@"Id"] = @"";
    //LLLog(@"%@",paramets);
    NSString *jsonStr = [self dictionaryToJson:paramets];
    [HttpRequest sendSelfPostRequestWithURL:@"/api/ShoppingCart/Add" WithParamets:jsonStr WithParamet:nil success:^(id result) {
        //LLLog(@"%@",result);
        NSDictionary*dic=(NSDictionary*)result;
        NSNumber*number=[dic objectForKey:@"code"];
        if ([number isEqual:@1]) {
            [SVProgressHUD showSuccessWithStatus:@"已加入购物车"];
            [SVProgressHUD dismissWithDelay:1.0];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        LLLog(@"error:%@",errer);
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
//立即购买
- (void)jumpBuy{
    //判断是登录以及开户
    [self isLoginAndOpenCount];
    if (_dictTwo) {
        if ([_dictTwo[@"isBuyEnable"] isEqualToString:@"Y"]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您将使用奕丰金融提供的基金销售服务" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                HDCFBuyFundVc *buyVc = [[HDCFBuyFundVc alloc] init];
                buyVc.dataDict = self.detailDict;
                buyVc.isCollcet = _isColect;
                buyVc.dictTwo = _dictTwo;
                buyVc.isLicai = _isLiCai;
                [self.navigationController pushViewController:buyVc animated:YES];
            }];
            UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction: action1];
            [alert addAction: action2];
            [self presentViewController:alert animated:NO completion:nil];
            
        }else{
            [SVProgressHUD showInfoWithStatus:@"已售磬"];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"您将使用奕丰金融提供的基金销售服务" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFBuyFundVc *buyVc = [[HDCFBuyFundVc alloc] init];
            buyVc.dataDict = self.detailDict;
            buyVc.isCollcet = _isColect;
            buyVc.isLicai = _isLiCai;
            [self.navigationController pushViewController:buyVc animated:YES];
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        
    }
}
-(void)isLoginAndOpenCount
{
    NSString *token = kToken;
    if (!(token.length > 0)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFLoginVc *loginVc = [[HDCFLoginVc alloc] init];
            [self.navigationController pushViewController:loginVc animated:NO];
            
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
    
    NSString *openStatus = kOpenStatus;
    if (!(openStatus.integerValue > 0)){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先开户！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFOpenAccountVc *loginVc = [[HDCFOpenAccountVc alloc] init];
            [self.navigationController pushViewController:loginVc animated:NO];
            
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
}
- (void)colecOrDeletClick{
    if (self.fundIsCollec == YES) {
        [self delColectFund];
    }else{
        [self colectFund];
    }
}

- (void)creatView{
    
    CGFloat oneHeight = 163;
    CGFloat twoHeight = 244;
    CGFloat threeHeight = 40 + 44 * 4 + 44 * 13;
    CGFloat fourHeight = 50;
    
    HDCFFundDetailDeader *header = [HDCFFundDetailDeader HDCFFundDetailDeader];
    header.isLiCai = _isLiCai;
    header.dataDict = self.detailDict;
    [header.collectionBtn addTarget:self action:@selector(colecOrDeletClick) forControlEvents:UIControlEventTouchUpInside];
    self.headder = header;
    header.frame = CGRectMake(0, 0, screenWidth, oneHeight);
    [self.mainScroll addSubview:header];
    
    HDCFFundDetailMiddleView *middleView = [HDCFFundDetailMiddleView HDCFFundDetailMiddleView];
    self.middleView = middleView;
    middleView.frame = CGRectMake(0, CGRectGetMaxY(header.frame), screenWidth, twoHeight);
    [self.mainScroll addSubview:middleView];
    
    CGRect middleTableFrame = CGRectMake(0, CGRectGetMaxY(middleView.frame), screenWidth, threeHeight);
    UITableView *middleTable = [[UITableView alloc] initWithFrame:middleTableFrame style:UITableViewStylePlain];
    middleTable.tag = 10;
    self.middleTable = middleTable;
    middleTable.delegate = self;
    middleTable.scrollEnabled = NO;
    middleTable.separatorColor = ZKcolor(245, 245, 245);
    middleTable.tableHeaderView =  [self creatHeader];
    middleTable.dataSource = self;
    [self.mainScroll addSubview:middleTable];
    
    UIButton *btnTwoYF = [[UIButton alloc] init];
    btnTwoYF.frame = CGRectMake(0, CGRectGetMaxY(middleTable.frame), screenWidth, 50);
    [btnTwoYF setBackgroundColor:ZKcolor(245, 245, 245)];
    NSString *strleft = @"基金销售服务由奕丰金融提供\n-基金有风险 投资需谨慎-";
    NSMutableAttributedString *attrStrT = [[NSMutableAttributedString alloc] initWithString:strleft];
    [attrStrT addAttribute:NSForegroundColorAttributeName
                     value:[UIColor lightGrayColor]
                     range:NSMakeRange(0, strleft.length)];
    [attrStrT addAttribute:NSFontAttributeName
                     value:[UIFont boldSystemFontOfSize:12]
                     range:NSMakeRange(0, strleft.length)];
    [attrStrT addAttribute:NSFontAttributeName
                     value:[UIFont boldSystemFontOfSize:14]
                     range:NSMakeRange(7, 4)];
    [attrStrT addAttribute:NSForegroundColorAttributeName
                     value:ZKcolor(33, 33, 33)
                     range:NSMakeRange(7, 4)];
    [btnTwoYF setAttributedTitle:attrStrT forState:UIControlStateNormal];
    [btnTwoYF setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnTwoYF.titleLabel setTextAlignment:NSTextAlignmentCenter];
    btnTwoYF.titleLabel.lineBreakMode = 0;
    [btnTwoYF addTarget:self action:@selector(presentPage) forControlEvents:UIControlEventTouchUpInside];
    [self.mainScroll addSubview:btnTwoYF];
    
    self.mainScroll.contentSize = CGSizeMake(screenWidth, oneHeight + twoHeight + threeHeight + 50);
}

- (void)presentPage{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *view = [[UIView alloc] init];
    self.clearHudView = view;
    view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    UITapGestureRecognizer *r5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewHit)];
    r5.numberOfTapsRequired = 1;
    [view addGestureRecognizer:r5];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(30, (screenHeight - (screenWidth - 60) * 900.0 / 500.0) / 2, screenWidth - 60, (screenWidth - 60) * 900.0 / 500.0);
    imageView.image = [UIImage imageNamed:@"661511947049_.pic_hd.jpg"];
    [view addSubview:imageView];
    view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
    [window addSubview:view];
}

- (void)viewHit{
    [self.clearHudView removeFromSuperview];
}

- (UIView *)creatHeader{
    NSArray *titleArray = [NSArray arrayWithObjects:@"阶段回报", @"本基金", @"同类平均", @"沪深300", nil];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 40)];
    CGFloat labelWidth = screenWidth / 4;
    for (int i = 0; i < 4; i++) {
        NSString *title = titleArray[i];
        UILabel *allMoney = [[UILabel alloc] init];
        allMoney.frame = CGRectMake(labelWidth * i, 5, labelWidth, 30);
        allMoney.text = title;
        allMoney.textColor = ZKcolor(32, 32, 32);
        allMoney.textAlignment = NSTextAlignmentCenter;
        allMoney.font = [UIFont systemFontOfSize:14];
        [view addSubview:allMoney];
    }

    UIView *slider = [[UIView alloc] init];
    slider.frame = CGRectMake(0, 39, screenWidth, 1);
    slider.backgroundColor = ZKcolor(245, 245, 245);
    [view addSubview:slider];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tableRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < (self.tableRows - 1)) {
        static NSString *cellID = @"cellidM";
        HDCFFundDetailMiddleCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [HDCFFundDetailMiddleCell HDCFFundDetailMiddleCell];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dictOne = self.selfEarnDict;
        cell.dictTwo = self.sameClassEarn;
        cell.dictThree = self.markitEarn;
        cell.index = indexPath.row;
        cell.preservesSuperviewLayoutMargins = NO;
        cell.separatorInset = UIEdgeInsetsZero;
        cell.layoutMargins = UIEdgeInsetsZero;
        return cell;
    }else{
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.preservesSuperviewLayoutMargins = NO;
        cell.separatorInset = UIEdgeInsetsZero;
        cell.layoutMargins = UIEdgeInsetsZero;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        btn.frame = CGRectMake(100, 6, screenWidth - 200, 30);
        if (self.tableRows == 4) {
            CGSize size = [self sizeWithText:@"查看更多" font:[UIFont systemFontOfSize:14] maxW:MAXFLOAT];
            [btn setImage:nil forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"gengduo"] forState:UIControlStateNormal];
            [btn setTitle:nil forState:UIControlStateNormal];
            [btn setTitle:@"查看更多" forState:UIControlStateNormal];
            [btn setTitleColor:ZKcolor(170, 170, 170) forState:UIControlStateNormal];
            UIImage *image = [UIImage imageNamed:@"gengduo"];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width - 10, 0, image.size.width)];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, size.width, 0, -size.width)];
        }else{
            CGSize size = [self sizeWithText:@"收起" font:[UIFont systemFontOfSize:14] maxW:MAXFLOAT];
            [btn setImage:nil forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
            [btn setTitle:nil forState:UIControlStateNormal];
            [btn setTitle:@"收起" forState:UIControlStateNormal];
            [btn setTitleColor:ZKcolor(170, 170, 170) forState:UIControlStateNormal];
            UIImage *image = [UIImage imageNamed:@"shouqi"];
            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width - 10, 0, image.size.width)];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, size.width, 0, -size.width)];
        }
    
        [cell addSubview:btn];
        [btn addTarget:self action:@selector(openTable:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)openTable:(UIButton *)btn{
    CGFloat oneHeight = 163;
    CGFloat twoHeight = 244;
    CGFloat threeHeight;
    if (self.tableRows == 4) {
        self.tableRows = 8;
        [self.middleTable reloadData];
        threeHeight = 40 + 44 * 8 + 44 * 13;
        self.middleTable.height = threeHeight;
        self.mainScroll.contentSize = CGSizeMake(screenWidth, oneHeight + twoHeight + threeHeight);
    }else{
        self.tableRows = 4;
        [self.middleTable reloadData];
        threeHeight = 40 + 44 * 4 + 44 * 13;
        self.middleTable.height = threeHeight;
        self.mainScroll.contentSize = CGSizeMake(screenWidth, oneHeight + twoHeight + threeHeight);
    }
    
}

//计算高度
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    CGRect rect = CGRectMake(0, 0, screenWidth, 44 * 14);
    HDCFFundDetailFooter *footer = [[HDCFFundDetailFooter alloc] initWithFrame:rect];
    footer.dataDict = self.detailDict;
    self.footer = footer;
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 44 * 13;
}
-(NSString *)geyTime
{
    NSDate *senddate = [NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *timeString=[dateformatter stringFromDate:senddate];
    return timeString;
}
@end
